# oa审批
from rest_framework.response import Response
from rest_framework.views import APIView
from approve.serializer import ApprovalTypeSer, ApprovalProcessSer, ApprovalProcessUserSer, ApprovalSer, \
    LeaveApprovalSer, LoanApprovalSer, ApprovalUserSer
from user.check_jwt import check_userjwt
from user.models import ApprovalProcessUser, ApprovalType, ApprovalProcess, Approval, LeaveApproval, LoanApproval,\
    ApprovalUser


# 审批类型
class ApprovalTypeInfoView(APIView):
    def get(self,request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        rank = ApprovalType.objects.all()
        ser = ApprovalTypeSer(rank,many=True)
        return Response({'code':200,'msg':ser.data})

    def post(self,request):
        approval_name = request.data.get('approval_name')
        last_edit = request.data.get('last_edit')
        last_time = request.data.get('last_time')
        template_id = request.data.get('template_id')
        approval_title = request.data.get('approval_title')
        approval = ApprovalType.objects.create(approval_name=approval_name,last_time=last_time,
                last_edit_id=last_edit,template_id_id=template_id,approval_title=approval_title,)
        approval.save()
        return Response({'code':200,'msg':'添加成功'})

class ApprovalTypeView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        rank = ApprovalType.objects.filter(id=pk).first()
        ser = ApprovalTypeSer(rank)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        ApprovalType.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        rank = ApprovalType.objects.filter(id=pk).first()
        ser = ApprovalTypeSer(instance=rank, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})


# 审批流程类型
class ApprovalProcessInfoView(APIView):
    def get(self,request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        rank = ApprovalProcess.objects.all()
        ser = ApprovalProcessSer(rank,many=True)
        return Response({'code':200,'msg':ser.data})

    def post(self,request):
        approvalProcess_type = request.data.get('approvalProcess_type')
        status = request.data.get('status')
        approvalProcess_illustrate = request.data.get('approvalProcess_illustrate')
        approvalpro = ApprovalProcess.objects.create(approvalProcess_type_id=approvalProcess_type,
                    status=status,approvalProcess_illustrate=approvalProcess_illustrate)
        approvalpro.save()
        return Response({'code':200,'msg':'添加成功'})

class ApprovalProcessView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        rank = ApprovalProcess.objects.filter(id=pk).first()
        ser = ApprovalProcessSer(rank)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        ApprovalProcess.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        rank = ApprovalProcess.objects.filter(id=pk).first()
        ser = ApprovalProcessSer(instance=rank, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})


# 审批人流程
class ApprovalProcessUserInfoView(APIView):
    def get(self,request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approvaluser = ApprovalProcessUser.objects.all()
        ser = ApprovalProcessUserSer(approvaluser,many=True)
        return Response({'code':200,'msg':ser.data})

    def post(self,request):
        approvalProcess_id = request.data.get('approvalProcess_id')
        approval_user = request.data.get('approval_user')
        num_user = request.data.get('num_user')
        approvalprocessuser = ApprovalProcessUser.objects.create(approvalProcess_id_id=approvalProcess_id,
                               approval_user_id=approval_user,num_user=num_user)
        approvalprocessuser.save()
        return Response({'code':200,'msg':'添加成功'})

class ApprovalProcessUserView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approvaluser = ApprovalProcessUser.objects.filter(id=pk).first()
        ser = ApprovalProcessUserSer(approvaluser)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        ApprovalProcessUser.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approvaluser = ApprovalProcessUser.objects.filter(id=pk).first()
        ser = ApprovalProcessUserSer(instance=approvaluser, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})


# 审批表
class ApprovalInfoView(APIView):
    def get(self,request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approval = Approval.objects.all()
        ser = ApprovalSer(approval,many=True)
        return Response({'code':200,'msg':ser.data})

    def post(self,request):
        applicant = request.data.get('applicant')
        applicant_user = request.data.get('applicant_user')
        approval_type = request.data.get('approval_type')
        approval_time = request.data.get('approval_time')
        now_approval = request.data.get('now_approval')
        now_approval_name = request.data.get('now_approval_name')
        status = request.data.get('status')
        reason = request.data.get('reason')
        approvalprocessuser = Approval.objects.create(applicant=applicant,applicant_user_id=applicant_user,approval_type_id=approval_type,
                            approval_time=approval_time,now_approval=now_approval,now_approval_name=now_approval_name,
                            status=status,reason=reason)
        approvalprocessuser.save()
        return Response({'code':200,'msg':'添加成功'})

class ApprovalView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approval = Approval.objects.filter(id=pk).first()
        ser = ApprovalSer(approval)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        Approval.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approval = Approval.objects.filter(id=pk).first()
        ser = ApprovalSer(instance=approval, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})


# 审批表请假
class LeaveApprovalInfoView(APIView):
    def get(self,request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        leaveapproval = LeaveApproval.objects.all()
        ser = LeaveApprovalSer(leaveapproval,many=True)
        return Response({'code':200,'msg':ser.data})

    def post(self,request):
        approval_id = request.data.get('approval_id')
        begin_time = request.data.get('begin_time')
        over_time = request.data.get('over_time')
        leaveapproval = LeaveApproval.objects.create(approval_id_id=approval_id,begin_time=begin_time,over_time=over_time)
        leaveapproval.save()
        return Response({'code':200,'msg':'添加成功'})

class LeaveApprovalView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        leaveapproval = LeaveApproval.objects.filter(id=pk).first()
        ser = LeaveApprovalSer(leaveapproval)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        LeaveApproval.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        leaveapproval = LeaveApproval.objects.filter(id=pk).first()
        ser = LeaveApprovalSer(instance=leaveapproval, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})


# 借款审批表
class LoanApprovalInfoView(APIView):
    def get(self,request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        oanApproval = LoanApproval.objects.all()
        ser = LoanApprovalSer(oanApproval,many=True)
        return Response({'code':200,'msg':ser.data})

    def post(self,request):
        approval_id = request.data.get('approval_id')
        begin_time = request.data.get('begin_time')
        loan_reason = request.data.get('loan_reason')
        loan_money = request.data.get('loan_money')
        oanApproval = LoanApproval.objects.create(approval_id_id=approval_id,begin_time=begin_time,loan_reason=loan_reason,loan_money=loan_money)
        oanApproval.save()
        return Response({'code':200,'msg':'添加成功'})

class LoanApprovalView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        leaveapproval = LoanApproval.objects.filter(id=pk).first()
        ser = LoanApprovalSer(leaveapproval)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        LoanApproval.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        leaveapproval = LoanApproval.objects.filter(id=pk).first()
        ser = LoanApprovalSer(instance=leaveapproval, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})


# 审批人
class ApprovalUserInfoView(APIView):
    def get(self, request):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approvaluser = ApprovalUser.objects.all()
        ser = ApprovalUserSer(approvaluser, many=True)
        return Response({'code': 200, 'msg': ser.data})

    def post(self, request):
        approval_user = request.data.get('approval_user')
        approval_id = request.data.get('approval_id')
        flag = request.data.get('flag')
        is_approval = request.data.get('is_approval')
        approvaluser = ApprovalUser.objects.create(approval_id_id=approval_id,approval_user_id=approval_user,
                                        flag=flag,is_approval=is_approval)
        approvaluser.save()
        return Response({'code': 200, 'msg': '添加成功'})

class ApprovalUserView(APIView):
    def get(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approvaluser = ApprovalUser.objects.filter(id=pk).first()
        ser = ApprovalUserSer(approvaluser)
        return Response({"code": 200, "msg": "获取成功", "data": ser.data})

    def delete(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        ApprovalUser.objects.filter(id=pk).delete()
        return Response({"code": 200, "msg": "删除成功"})

    def put(self, request, pk):
        token = request.headers.get("Token")
        payload = check_userjwt(token)
        if not payload:
            return Response({"code": 401, "msg": "请先登录"})
        approvaluser = ApprovalUser.objects.filter(id=pk).first()
        ser = ApprovalUserSer(instance=approvaluser, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({'code': 200, 'msg': '更新成功'})



